package com.zaixiandiancan.xia.Controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zaixiandiancan.xia.entity.User;
import com.zaixiandiancan.xia.service.UserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.util.DigestUtils;
import org.springframework.util.ResourceUtils;

@Controller
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    Map<String,Object> map = new HashMap<>();

    @RequestMapping("/login_index")
    public String login_index(){
        return "login";
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/goods")
    public String goods(){
        return "goods";
    }

    @RequestMapping("/orders")
    public String orders(){
        return "orders";
    }

    @RequestMapping("/ercode")
    public String ercode(){
        return "ercode";
    }

    @RequestMapping("/person")
    public String person(){
        return "person";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Map<String,Object> main(@RequestBody User user, HttpServletRequest request) {
        map.clear();
        HttpSession session = request.getSession();
        List<User> list = new ArrayList<>();
        try {
            String pass = DigestUtils.md5DigestAsHex(user.getPassWord().getBytes());
            list = userService.searchUserByName(user);
            if (list.size() == 1)
            {
                User u = list.get(0);
                String passWord = u.getPassWord();
                if (pass.equals(passWord))
                {
                    session.setAttribute("users", u);
                    session.setMaxInactiveInterval(30*60);
                    map.put("code", 0);
                    map.put("username", u.getUserName());
                    map.put("userid", u.getId().toString());
                    map.put("msg", "success");
                    map.put("msg", "登录成功");
                }else {
                    map.put("code", 1);
                    map.put("msg", "failed");
                    map.put("msg", "用户名或者密码错误");
                }

            }else {
                map.put("code", 1);
                map.put("msg", "failed");
                map.put("msg", "用户不存在");
            }
        } catch (Exception e) {
            map.put("code", 1);
            map.put("msg", "failed");
            map.put("msg", "登录异常");
        }
        return map;
    }



}
